#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import mutagen
import os
import re

# 支持的音频文件扩展名
audio_extensions = ('.mp3', '.flac')

def fix_artist(file_path, new_artist_name):
    """
    修复音频文件的艺术家标签。
    
    :param file_path: 音频文件的完整路径
    :param new_artist_name: 新的艺术家名称
    """
    if file_path.endswith(audio_extensions):
        try:
            audio = mutagen.File(file_path)
            if 'artist' in audio and audio['artist'][0] == new_artist_name:
                return
            # 设置新的艺术家名称
            audio['artist'] = [new_artist_name]
            audio.save()
            print(f'Fixed artist for {file_path} to {new_artist_name}')
            # else:
            #     print(f'No artist found for {file_path}')
        except Exception as e:
            print(f'Error fixing artist for {file_path}: {e}')

if __name__ == '__main__':
    # 指定要修改的文件夹路径
    directory_path = r'H:\demo'
    # 设置新的艺术家名称
    new_artist_name = '刀郎'
    
    # 遍历目录中的音频文件
    for root, dirs, files in os.walk(directory_path):
        for file in files:
            if file.endswith(audio_extensions):
                file_path = os.path.join(root, file)
                # 修改艺术家标签
                fix_artist(file_path, new_artist_name)
